from sqlalchemy.orm import Session
from schema import Server_Status_schema
from model import Server_Status_model



def get_Server_Status(db: Session, id: int):
    return db.query(Server_Status_model.Server_status).filter(Server_Status_model.Server_status.id == id).first()


def get_all_Server_Status(db: Session):
    return db.query(Server_Status_model.Server_status).all()


def delete_Server_Status(db: Session, id):
    to_delete = db.query(Server_Status_model.Server_status).filter(Server_Status_model.Server_status.id == id)
    deleted = to_delete.delete()
    if deleted == 0:
        return None
    else:
        db.commit()
        return True


def create_Server_Status(db: Session, Server_Status: Server_Status_schema.Server_status):
    db_Server_Status = Server_Status_model.Server_status(
        cpu_usage=Server_Status.cpu_usage,
        memory_usage=Server_Status.memory_usage,
        ai_process_count=Server_Status.ai_process_count,
        web_frontend_status=Server_Status.web_frontend_status,
        web_backend_status=Server_Status.web_backend_status,
        disk_usage_percent=Server_Status.disk_usage_percent,
        disk_utilization_percent=Server_Status.disk_utilization_percent,
        network_bandwidth=Server_Status.network_bandwidth,
        network_throughput=Server_Status.network_throughput,
        system_load_average_percent=Server_Status.system_load_average_percent,
        gpu_model=Server_Status.gpu_model,
        gpu_utilization=Server_Status.gpu_utilization,
        gpu_memory_free=Server_Status.gpu_memory_free,
        gpu_temperature=Server_Status.gpu_temperature,
        timestamp=Server_Status.timestamp,
    )
    db.add(db_Server_Status)
    db.commit()
    db.refresh(db_Server_Status)
    return db_Server_Status


def update_Server_Status(db: Session, id, field_name, new_value):
    db_Server_Status = db.query(Server_Status_model.Server_status).filter(Server_Status_model.Server_status.id == id).first()
    setattr(db_Server_Status, field_name, new_value)
    db.commit()
    db.refresh(db_Server_Status)
    return db_Server_Status
